package com.laxcen.controller2;

import com.aliyun.openservices.log.exception.LogException;
import com.github.pagehelper.PageInfo;
import com.laxcen.smoke.dto.req.LogAppReq;
import com.laxcen.smoke.dto.req.PageInfoReq;
import com.laxcen.smoke.dto.res.LogWebRes;
import com.laxcen.smoke.service.LogService;
import com.laxcen.smoke.util.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

@Api(tags = "日志管理")
@RestController
@RequestMapping("/logmanage")
public class LogController {

    @Autowired
    private LogService logService;

    @ApiOperation(value = "获取web日志",notes =
            "请求信息： \n" +
                    "&emsp; pageNo &nbsp; : &nbsp; 当前页数 \n" +
                    "&emsp; pageSize &nbsp; : &nbsp; 每页的数据条数 \n" +
                    "返回信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; id \n" +
                    "&emsp; username &nbsp; : &nbsp; 操作人 \n" +
                    "&emsp; operation &nbsp; : &nbsp; 操作说明 \n" +
                    "&emsp; opurl &nbsp; : &nbsp; 操作接口路径 \n" +
                    "&emsp; deviceno &nbsp; : &nbsp; 设备编号 \n" +
                    "&emsp; createtime &nbsp; : &nbsp; 创建时间 \n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/getweblog")
    @RequiresPermissions("logmanage")
    public Response<PageInfo<LogWebRes>> getWebLog(@RequestBody PageInfoReq pageInfoReq){
        if (pageInfoReq.getPageNo()==null){
            pageInfoReq.setPageNo(1);
        }
        if (pageInfoReq.getPageSize()==null){
            pageInfoReq.setPageSize(10);
        }
        if (pageInfoReq.getPageNo()==null&&pageInfoReq.getPageSize()==null){
            pageInfoReq.setPageNo(1);
            pageInfoReq.setPageSize(10);
        }
        return logService.getWebLogList(pageInfoReq.getPageNo(),pageInfoReq.getPageSize());
    }

    @ApiOperation(value = "获取app日志",notes =
            "请求信息： \n" +
                    "&emsp; starttime &nbsp; : &nbsp; 时间左区间 \n" +
                    "&emsp; endtime &nbsp; : &nbsp; 时间右区间 \n" +
                    "&emsp; devicename &nbsp; : &nbsp; devicename \n" +
                    "返回信息： \n" +
                    "&emsp; traceId &nbsp; : &nbsp; TraceID \n" +
                    "&emsp; reason &nbsp; : &nbsp; reason \n" +
                    "&emsp; __topic__ &nbsp; : &nbsp; topic \n" +
                    "&emsp; code &nbsp; : &nbsp; code \n" +
                    "&emsp; resultData &nbsp; : &nbsp; resultData \n" +
                    "&emsp; bizCode &nbsp; : &nbsp; 业务类型(device设备行为 uplink设备到云消息 ThingModel物模型数据上报) \n" +
                    "&emsp; messageId &nbsp; : &nbsp; messageId \n" +
                    "&emsp; productKey &nbsp; : &nbsp; productKey \n" +
                    "&emsp; params &nbsp; : &nbsp; params \n" +
                    "&emsp; deviceName &nbsp; : &nbsp; deviceName \n" +
                    "&emsp; content &nbsp; : &nbsp; 内容 \n" +
                    "&emsp; instanceId &nbsp; : &nbsp; instanceId \n" +
                    "&emsp; utcTime &nbsp; : &nbsp; 时间 \n" +
                    "&emsp; operation &nbsp; : &nbsp; 操作 \n" +
                    "&emsp; status &nbsp; : &nbsp; 状态 \n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/getapplog")
    @RequiresPermissions("logmanage")
    public Response<List<Map>> getAppLog(@RequestBody LogAppReq logAppReq) throws LogException, ParseException {
        return logService.getAppLogList(logAppReq.getStarttime(),logAppReq.getEndtime(),logAppReq.getDevicename());
    }




}
